<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
use Zizaco\Entrust\HasRole;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use HasRole; // Add this trait to your user model

    protected $fillable = array(
        'id',
        'passwd',
        'usertype',
        'userstatus',
        'saint',
        'username',
        'firstname',
        'gender',
        'birthday',
        'usercrew',
        'baptism_date',
        'eucharist_date',
        'confirmation_date',
        'email',
        'phone',
        'address',
        'parish',
        'gencode',
        'note',
        'updater'
    );

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'user';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('passwd');

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier() {
        return $this->getKey();
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword() {
        return $this->passwd;
    }

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail() {
        return $this->email;
    }

    public function getRememberToken() {
        return $this->remember_token;
    }

    public function setRememberToken($value) {
        $this->remember_token = $value;
    }

    public function getRememberTokenName() {
        return 'remember_token';
    }

    public function scopeHuynhTruong($query) {
        $query->where('usertype', 'HUYNH_TRUONG');

        return $query;
    }

    public function scopeThieuNhi($query) {
        $query->where('usertype', 'THIEU_NHI');

        return $query;
    }

    public function isThieuNhi() {
        return $this->usertype == 'THIEU_NHI';
    }

    public function classes() {
        return $this->belongsToMany('Classes', 'user_class', 'user_id', 'class_id')->withTimestamps()->withPivot('id', 'chuyencan', 'hocluc', 'rank', 'note', 'updater');
    }

    public function roles() {
        return $this->belongsToMany('Role', 'assigned_roles', 'user_id', 'role_id')->withTimestamps()->withPivot('id', 'updater');
    }

    public function dears() {
        return $this->hasMany('UserDear', 'user_id');
    }

    public function score() {
        return $this->hasMany('UserScore', 'user_id');
    }

    public function scorelog() {
        return $this->hasMany('UserScoreLog', 'user_id');
    }

}
